package com.hup.utils.swing.screenShot;

import com.hup.utils.commons.exception.ErrInfo;
import com.hup.utils.commons.hardware.ScreenUtil;
import com.hup.utils.commons.start.DialogHelper;
import lombok.AccessLevel;
import lombok.NoArgsConstructor;
import lombok.extern.slf4j.Slf4j;

import java.awt.AWTException;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
import java.awt.Rectangle;
import java.awt.Robot;
import java.awt.image.BufferedImage;

/**
 * 截屏工具类
 * @author hugan
 * @date 2020/4/15
 */
@Slf4j
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public class ScreenShotUtil {

    protected static Robot robot;

    public static BufferedImage getScreenImage(boolean includeSidebar) {
        if (robot == null) {
            try {
                robot = new Robot();
            } catch (AWTException e) {
                ErrInfo errInfo = DialogHelper.show("初始化错误?:\n", e, null);
                throw new IllegalArgumentException(errInfo.getErrMsg());
            }
        }
        Rectangle rect;
        if (includeSidebar) {
            Dimension size = ScreenUtil.getScreenSize();
            rect = new Rectangle(0, 0, size.width, size.height);
        } else {
            rect = ScreenUtil.getScreenUsableBounds();
        }
        return robot.createScreenCapture(rect);
    }

    public static BufferedImage getAllScreenImage(boolean includeSidebar) {
        if (robot == null) {
            try {
                robot = new Robot();
            } catch (AWTException e) {
                ErrInfo errInfo = DialogHelper.show("初始化错误?:\n", e, null);
                throw new IllegalArgumentException(errInfo.getErrMsg());
            }
        }

        GraphicsDevice[] devices = GraphicsEnvironment.getLocalGraphicsEnvironment().getScreenDevices();
        if (devices.length < 2) {
            return getScreenImage(includeSidebar);
        }
        return robot.createScreenCapture(ScreenUtil.getAllScreenRect());
    }

    public static void shotColor(ShotHandler<Color> handler) {
        shotColor(true, handler);
    }

    public static void shotColor(boolean includeSidebar, ShotHandler<Color> handler) {
        ScreenShotFrame.show(ShotType.COLOR, includeSidebar, handler);
    }

    public static void shotImage(ShotHandler<BufferedImage> handler) {
        shotImage(true, handler);
    }

    public static void shotImage(boolean includeSidebar, ShotHandler<BufferedImage> handler) {
        ScreenShotFrame.show(ShotType.IMAGE, includeSidebar, handler);
    }

    /**
     * 截屏类型
     */
    protected enum ShotType {
        /**
         * 从屏幕图像中吸取某个像素的颜色
         */
        COLOR("颜色吸取", 1),
        /**
         * 从屏幕图像中截取某范围的图像
         */
        IMAGE("截屏", 2)
        ;

        public final String title;
        /**
         * 提交结果时,左键点击的次数
         */
        public final int applyClickCount;

        ShotType(String title, int applyClickCount) {
            this.title = title;
            this.applyClickCount = applyClickCount;
        }
    }

    public interface ShotHandler<T> {

        /**
         * 设置截屏窗口的标题
         * @return 默认是 {@link ShotType#title}
         */
        default String getTitle(ShotType type) {
            return type.title;
        }

        /**
         * @param res 被选中的目标[图像,颜色]
         * @param x 屏幕中的坐标
         * @param wid 图像尺寸
         */
        void onSelected(T res, int x, int y, int wid, int hei);
    }

}
